package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.data.model.CreditCardIndexModel;
import com.kbbywyclient.data.model.LoanIndexModel;
import com.kbbywyclient.data.model.SalesSettingModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.MainContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by FHW
 * on 2017/3/21
 */

public class MainPresenter extends BasePresenter implements MainContract.Presenter {
    private MainContract.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    //为注解了@Inject的对象注入无参构造方法构造的对象
    @Inject
    public MainPresenter(MainContract.View view) {
        this.view = view;

    }


    @Override
    public void getLoanIndex(String salesID,boolean showLoading) {
        rsaJsonUtil.put("SalesID", salesID);
        if(showLoading){
            view.showLoadingDialog(mContext.getString(R.string.text_loading));
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getLoanIndex(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<LoanIndexModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(LoanIndexModel loanIndexModel) {
                        view.loadSucLoanIndex(loanIndexModel.getData());
                    }
                });

    }

    @Override
    public void getCreditCardIndex(String saleID,boolean showLoading) {
        rsaJsonUtil.put("SalesID", saleID);
        if(showLoading){
            view.showLoadingDialog(mContext.getString(R.string.text_loading));
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getCreditCardIndex(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<CreditCardIndexModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(CreditCardIndexModel creditCardIndexModel) {
                        view.loadSucCreditIndex(creditCardIndexModel.getData());
                    }
                });
    }

    @Override
    public void getSalesSetting(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
//        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getSalesSetting(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<SalesSettingModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(SalesSettingModel salesSettingModel) {
                        view.loadSucSaleSetting(salesSettingModel.getData());
                    }
                });
    }

}
